var async = require('async');
var categoryDAL = require('../DAL/categoryDAL');
var collectDAL = require('../DAL/collectDAL');

function collectController(app) {
	//用户分页收藏页面
	app.get('/member/collect/:id', function(req, res) {
		var currentpage = parseInt(req.params.id);
		//验证登录
		var userInfo = req.session.customer;
		if(!userInfo) {
			res.redirect('/login');
		}
		var customer_Id = userInfo.customer_Id;

		//分页方法
		//定义分页参数
		//var currentpage = 1; //当前页
		var pagercounts = 1; //页面总数
		var previouspage = 1; //上一页
		var nextpage = 1;
		//设置分页 每页显示数据条数
		var pageSize = 10;
		//应用async模块的api:parallel处理并行无关联操作
		async.parallel({ //控制流程1:多级菜单加载
			categorylist: function(done) {
				//处理逻辑
				categoryDAL.getCategoryAll(function(catelist) {
					//循环构造数组对象模型
					var categorylist = create_objectArrayList(catelist, 0);
					done(null, categorylist);
				});
			}, //控制流程2:获取收藏分页信息
			collectlist: function(done) {
				collectDAL.getCollectListPager(customer_Id, currentpage, pageSize, function(collectlist) {
					//console.log(collectlist);
					done(null, collectlist);
				});
			}, //控制流程3:获取收藏行数
			collectcounts: function(done) {
				collectDAL.getCollectCounts(customer_Id, function(count) {
					done(null, count);
				});
			}
		}, function(err, results) {
			//将上面两步数据操作并行处理，然后在这里一起操作，提供运行效率
			var counts = parseInt(results.collectcounts);
			//分页信息处理
			//计算总页数
			if((counts % pageSize) > 0) {
				//有余数，数据不足一整页
				pagercounts = parseInt(counts / pageSize) + 1;
			} else {
				//数据正好填满整页
				pagercounts = parseInt(counts / pageSize);
			}
			//设置上一页
			if(currentpage <= 1) {
				previouspage = 1;
			} else {
				previouspage = currentpage - 1;
			}
			//设置下一页
			if(currentpage >= pagercounts) {
				nextpage = pagercounts;
			} else {
				nextpage = currentpage + 1;
			}

			console.log(results.product);
			//res.setHeader(200, {"Content-Type": "text/html"});
			res.render('member/member_collect', {
				categorylist: results.categorylist,
				collectlist: results.collectlist,
				currentpage: currentpage,
				previouspage: previouspage,
				nextpage: nextpage,
				pageSum: pagercounts,
				userInfo: userInfo
			});

		});
	});

	//用户收藏页面
	app.get('/member/collect', function(req, res) {
		res.redirect('/member/collect/1');
	});

	//将指定商品从收藏列表移除
	app.get('/collect/removeCollecthandle/:id', function(req, res) {
		//验证登录
		var userInfo = req.session.customer;
		if(!userInfo) {
			res.redirect('/login');
		}

		var product_Id = req.params.id;
		var customer_Id = userInfo.customer_Id;
		//处理方法
		collectDAL.removeCollect(customer_Id, product_Id, function(results) {
			if(results) {
				//删除成功
				res.redirect('/member/collect');
			} else {
				//删除失败
				res.redirect('/member/collect');
			}
		});
	});

	//添加收藏方法
	//ajax请求
	app.post('/collect/addCollecthandle', function(req, res) {
		//验证登录
		var userInfo = req.session.customer;
		if(!userInfo) {
			res.send({
				url: '/login',
				message: '未登录'
			});
		}
		var product_Id = req.body.id;
		var customer_Id = userInfo.customer_Id;

		//处理方法
		collectDAL.addCollect(customer_Id, product_Id, function(results, message) {
			if(results) {
				//添加成功                
				res.send({
					url: '/',
					message: message
				});
			} else {
				//已经存在
				res.send({
					url: '/',
					message: message
				});
			}
		});
	});

	//递归构造树型数组对象
	function create_objectArrayList(data, pid) {
		pid = pid || 0;
		var list = [];
		data.map(function(item) {
			if(item.up_id == pid) {
				item.list = create_objectArrayList(data, item.category_Id);
				list.push(item);
			}
		});
		return list;
	}
}

module.exports = collectController;